//
//  ChoiceFooterView.swift
//  cxd4iphone
//
//  Created by hexy on 11/28/15.
//  Copyright © 2015 hexy. All rights reserved.
//

import UIKit

private struct kConstraints {
    
    static let cornerRadius: CGFloat = 8
    static let wrapEdges: UIEdgeInsets = UIEdgeInsetsMake(25, 40, -20, -40)
    static let xRedColor: UIColor = xx_colorWithHex(hexValue: 0xf64242)
    static let xBlueColor: UIColor = xx_colorWithHex(hexValue: 0x3d64b4)
    static let lightFontColor: UIColor = xx_colorWithHex(hexValue: 0x666666)
    static let darkFontColor: UIColor = xx_colorWithHex(hexValue: 0x333333)
    static let progressHeight: CGFloat = xx_height(12)
}

class ChoiceFooterView: UIView {

    var bidViewModel: BidViewModel? {
        
        didSet {
            
            let vm = bidViewModel!
            beginInvestNumLabel.text = vm.beiginInvestNum
            investLastNumLabel.text = vm.investLastNum
            investLastUnitLabel.text = vm.investLastUnit
            progressView.bidViewModel = vm
            rateNumLabel.text = vm.bidModel.rate ?? "67"
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        makeUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func makeUI() {
        
        backgroundColor = UIColor.whiteColor()
        
        addSubview(wrap)
        wrap.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(kConstraints.wrapEdges)
        }
        
        wrap.addSubview(topBtn)
        topBtn.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(wrap)
            make.top.equalTo(wrap).offset(-7)
        }
        
        let rateWrap = UIView(bgColor: UIColor.lightGrayColor())
        
        wrap.addSubview(rateWrap)
        rateWrap.snp_makeConstraints { (make) -> Void in
            
            var marginTop: CGFloat = 0
            
            if xx_iPhone4() {
                marginTop = xx_height(-8)
            }
            if xx_iPhone5() {
                
                marginTop = xx_height(0)
            }
            
            if xx_iPhone6() {
                marginTop = xx_height(2)
            }
            
            if xx_iPhone6p() {
                
                marginTop = xx_height(0)
            }
            make.top.equalTo(topBtn.snp_bottom).offset(marginTop)
            make.centerX.equalTo(wrap)
        }
        
        rateWrap.addSubview(rateNumLabel)
//        rateNumLabel.backgroundColor = xx_randomColor()
        rateNumLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.left.equalTo(rateWrap)
            
        }
        
//        percentLabel.backgroundColor = xx_randomColor()
        rateWrap.addSubview(percentLabel)
        percentLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(rateNumLabel.snp_right)
            make.right.equalTo(rateWrap)
            
            var centerYoffset: CGFloat = 0
            if xx_iPhone4() {
                
                centerYoffset = xx_folat(19)
            }
            if xx_iPhone5() {
                
                centerYoffset = xx_folat(19)
            }

            if xx_iPhone6() {
                
                centerYoffset = xx_folat(23)
            }
            if xx_iPhone6p() {
                
                centerYoffset = xx_folat(25)
            }

            make.centerY.equalTo(rateNumLabel).offset(centerYoffset)
        }
        
        let yearRateLabel = UILabel(title: "年化收益率", fontSize: 12)
        
        rateWrap.addSubview(yearRateLabel)
        yearRateLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(percentLabel.snp_bottom)//.offset(xx_height(-10))
            make.centerX.equalTo(rateWrap)
        }
        
        let bottomWrap = UIView(bgColor: Define.blueColor)
        
        bottomWrap.layer.cornerRadius = kConstraints.cornerRadius
        
        wrap.addSubview(bottomWrap)
        bottomWrap.snp_makeConstraints { (make) -> Void in
            
            make.left.bottom.right.equalTo(wrap)
            make.height.equalTo(xx_height(60))
        }
        let tipWrap = UIView(bgColor: UIColor.whiteColor())
        //        let tipWrap = UIView(bgColor: UIColor.lightGrayColor())
        
        bottomWrap.addSubview(tipWrap)
        tipWrap.snp_makeConstraints { (make) -> Void in
            
            make.top.left.right.equalTo(bottomWrap)
            make.height.equalTo(xx_height(20))
        }
        
        tipWrap.addSubview(beginInvestNumLabel)
        beginInvestNumLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(tipWrap).offset(16)
            make.centerY.equalTo(tipWrap)
        }
        
        
        let beginInvestLabel = UILabel(title: "元起投", fontSize: 12)
        
        tipWrap.addSubview(beginInvestLabel)
        beginInvestLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(beginInvestNumLabel.snp_right)
            make.centerY.equalTo(tipWrap)
        }
        
        tipWrap.addSubview(investLastUnitLabel)
        investLastUnitLabel.snp_makeConstraints { (make) -> Void in
            
            make.right.equalTo(tipWrap).offset(-15)
            make.centerY.equalTo(tipWrap)
        }
        
        tipWrap.addSubview(investLastNumLabel)
        investLastNumLabel.textColor = Define.redColor
        investLastNumLabel.snp_makeConstraints { (make) -> Void in
            
            make.right.equalTo(investLastUnitLabel.snp_left)
            make.centerY.equalTo(tipWrap)
        }
        
        
        
        
        bottomWrap.addSubview(profitBtn)
        profitBtn.snp_makeConstraints { (make) -> Void in
            
            make.left.bottom.right.equalTo(bottomWrap)
            make.height.equalTo(xx_height(40))
        }
        
//        let progressWrap = UIView(bgColor: UIColor.orangeColor())
        let progressWrap = UIView(bgColor: UIColor.whiteColor())
        
        wrap.addSubview(progressWrap)
        progressWrap.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(wrap).offset(40)
            make.right.equalTo(wrap).offset(-40)
            make.bottom.equalTo(bottomWrap.snp_top)
            
            var height: CGFloat = 0
            if xx_iPhone4() {
                
                height = xx_height(30)
            }
            
            if xx_iPhone5() {
                
                height = xx_height(45)
            }
            
            if xx_iPhone6() {
                
                height = xx_height(50)
            }
            if xx_iPhone6p() {
                
                height = xx_height(55)
            }
            make.height.equalTo(height)
        }

        progressWrap.addSubview(progressView)
        progressView.snp_makeConstraints { (make) -> Void in

            make.left.right.equalTo(progressWrap)
            make.centerY.equalTo(progressWrap)
        }
        
   

    }
    
    lazy var beginInvestNumLabel = UILabel(num: "100", fontSize: 14)
    lazy var investLastNumLabel = UILabel(num: "8", fontSize: 16)
    lazy var investLastUnitLabel = UILabel(num: "天", fontSize: 12)
    lazy var rateNumLabel:UILabel = {
        
        var fontSize: CGFloat = 0
        
        if xx_iPhone4() {
            
            fontSize = 90
        }
        if xx_iPhone5() {
            
            fontSize = 110
        }
        
        if xx_iPhone6() {
            
            fontSize = 120
        }
        
        if xx_iPhone6p() {
            
            fontSize = 130
            
        }
        return UILabel(num: "20", fontSize: fontSize, fontColor: Define.redColor)
       
    }()
    lazy var percentLabel:UILabel = {
        
        var fontSize: CGFloat = 0
        
        if xx_iPhone4() {
            
            fontSize = 35
        }
        if xx_iPhone5() {
            
            fontSize = 50
        }
        if xx_iPhone6() {
            
            fontSize = 60
        }
        if xx_iPhone6p() {
            
            fontSize = 60
        }

        return UILabel(num: "%", fontSize: fontSize, fontColor: Define.redColor)
    }()
    
    
    lazy var progressView = ProgressView(height: xx_height(12))


    
    lazy var profitBtn: UIButton = {
        
        let btn = UIButton(title: "马上赚钱", titleColor: UIColor.whiteColor(), titleSize: 20, bgColor: UIColor.clearColor())
        btn.layer.cornerRadius = 8
        btn.clipsToBounds = true
        return btn
    }()
    
    lazy var topBtn: UIButton = {
        
        let topBtn = UIButton(title: "热门推荐", titleColor: UIColor.whiteColor(), titleSize: 12, bgColor: UIColor.clearColor())
        topBtn.setBackgroundImage(UIImage(named: "choice_topImage"), forState: .Normal)
        topBtn.sizeToFit()
        return topBtn
    }()

    lazy var wrap: UIView = {
        
        let wrap = UIView()
        wrap.backgroundColor = UIColor.whiteColor()
        wrap.layer.cornerRadius = kConstraints.cornerRadius
        wrap.layer.shadowColor = UIColor.blackColor().CGColor
        wrap.layer.shadowOffset = CGSizeMake(0.0, 0.0)
        wrap.layer.shadowOpacity = 0.2
        wrap.layer.shadowRadius = 6
        return wrap
    }()
    
    
    
    
}

